﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace OCR
{
    public partial class frmMain : Form
    {
        //状态相关
        [DllImport("FreqDLL.dll", EntryPoint = "GetPowerCount", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetPowerCount();
        [DllImport("FreqDLL.dll", EntryPoint = "GetPower", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetPower(int Index);
        [DllImport("FreqDLL.dll", EntryPoint = "GetPower1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetPower1();
        [DllImport("FreqDLL.dll", EntryPoint = "GetPower2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetPower2();

        //频率相关
        [DllImport("FreqDLL.dll", EntryPoint = "GetFreqCount", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetFreqCount();
        [DllImport("FreqDLL.dll", EntryPoint = "GetFreq", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetFreq(int Index);
        [DllImport("FreqDLL.dll", EntryPoint = "GetFreq1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetFreq1();
        [DllImport("FreqDLL.dll", EntryPoint = "GetFreq2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetFreq2();

        //计数相关
        [DllImport("FreqDLL.dll", EntryPoint = "InitCount", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void InitCount();
        [DllImport("FreqDLL.dll", EntryPoint = "GetCountCount", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetCountCount();
        [DllImport("FreqDLL.dll", EntryPoint = "GetCount", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetCount(int Index);
        [DllImport("FreqDLL.dll", EntryPoint = "GetCount1", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetCount1();
        [DllImport("FreqDLL.dll", EntryPoint = "GetCount2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetCount2();

        [DllImport("FreqDLL.dll", EntryPoint = "GetIDEx", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetIDEx(int Index, [MarshalAs(UnmanagedType.LPStr)]StringBuilder APChar,
            ref int APCharLen);
        //定义委托
        public delegate void CallbackDelegate(int index, int count);
        public static CallbackDelegate callback;

        [DllImport("FreqDLL.dll", EntryPoint = "SetCount", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
        public static extern void SetCount(CallbackDelegate callback);

        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void btnGetTempCount_Click(object sender, EventArgs e)
        {
            richTxtBoxTemp.AppendText("状态探头数：" + GetPowerCount().ToString() + "\n");
        }

        private void btnGetTemp1_Click(object sender, EventArgs e)
        {
            richTxtBoxTemp.AppendText("探头1状态：" + (GetPower1()).ToString() + "\n");
        }

        private void btnGetTemp2_Click(object sender, EventArgs e)
        {
            richTxtBoxTemp.AppendText("探头2状态：" + (GetPower2()).ToString() + "\n");
        }

        private void btnGetTempN_Click(object sender, EventArgs e)
        {
            richTxtBoxTemp.AppendText("探头" + txtBoxTempN.Text + "状态：" + (GetPower(Convert.ToInt16(txtBoxTempN.Text))).ToString() + "\n");
        }

        private void btnTempC_Click(object sender, EventArgs e)
        {
            richTxtBoxTemp.Text = "";
        }

        private void btnGetHumidity1_Click(object sender, EventArgs e)
        {
            richTxtBoxHumidity.AppendText("探头1频率：" + (Convert.ToDouble(GetFreq1())).ToString() + "\n");
        }

        private void btnGetHumidity2_Click(object sender, EventArgs e)
        {
            richTxtBoxHumidity.AppendText("探头2频率：" + (Convert.ToDouble(GetFreq2())).ToString() + "\n");
        }

        private void btnGetHumidityCount_Click(object sender, EventArgs e)
        {
            richTxtBoxHumidity.AppendText("频率探头数：" + GetFreqCount().ToString() + "\n");
        }

        private void linkLabelTaoBao_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://goxinyue.taobao.com/");
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            richTxtBoxHumidity.AppendText("探头" + textBox1.Text + "频率：" + (GetFreq(Convert.ToInt16(textBox1.Text))).ToString() + "\n");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText("探头" + textBox2.Text + "计数：" + (GetCount(Convert.ToInt16(textBox2.Text))).ToString() + "\n");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText("计数探头数：" + GetCountCount().ToString() + "\n");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText("探头1计数：" + (Convert.ToDouble(GetCount1())).ToString() + "\n");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            richTextBox1.AppendText("探头2计数：" + (Convert.ToDouble(GetCount2())).ToString() + "\n");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            InitCount();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int len = 1000;
            //string s2 = new string((char)0, l);
            StringBuilder sb = new StringBuilder(len);
            GetIDEx(1, sb, ref len);
            richTextBox1.AppendText(sb.ToString());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            SetCount(ShowCount);
        }
        private void ShowCount(int index,int value)
        {
            this.richTextBox1.Text += "探头:" + index.ToString() + ";计数:" + value.ToString() + Environment.NewLine;
        }
    }
}
